import scrapy


class QingtingSpider(scrapy.Spider):
    name = 'qingting'  # 爬虫的名字
    allowed_domains = ['qingting.fm']  # 允许的域名
    start_urls = ['https://m.qingting.fm/rank/']  # 开始爬取的URL

    def parse(self, response):
        """对start_urls中的URL爬取之后，回调的函数"""
        # print("----->", response)
        # print("---1-->", response.url)
        # print("---2-->", response.headers)
        # print("---3-->", response.status)
        # print("---4-->", response.body)
        # print("---5-->", response.request.url)
        # print("---6-->", response.request.headers)

        # 提取30个需要的a标签对象
        a_list = response.xpath("//div[@class='rank-list']//a")
        print(type(a_list))  # <class 'scrapy.selector.unified.SelectorList'>

        # 遍历取到的数据，对每个单独提取需要的数据
        for a_temp in a_list:
            rank_number = a_temp.xpath("./div[@class='badge']//text()").extract_first()  # 排名
            img_url = a_temp.xpath("./img/@src").extract_first()  # 图片地址
            title = a_temp.xpath("./div[@class='content']/div[@class='title']/text()").extract_first()  # 标题
            desc = a_temp.xpath("./div[@class='content']/div[@class='desc']/text()").extract_first()  # 描述
            play_number = a_temp.xpath(".//div[@class='info-item'][1]/span/text()").extract_first()  # 播放量
            print("--**-->", rank_number, img_url, title, desc, play_number)

            # ----- 修改的代码---开始----
            temp_dict = {
                "rank_number": rank_number,
                "img_url": img_url,
                "title": title,
                "desc": desc,
                "play_number": play_number
            }
            yield temp_dict
            # ----- 修改的代码---结束----
